{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python 机器学习实战 ——代码样例\n",
    "\n",
    "# 第十八章 K-Means 聚类"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 使用 K-Means聚类分析\n",
    "\n",
    "本节我们将通过一个例子演示使用 Scikit-Learn包进行 K-Means聚类，并把结果可视化。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.collections.PathCollection at 0x17668c0def0>"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAlMAAAJZCAYAAACN2rCOAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAGSdJREFUeJzt3WuMpGl53+H/DTMTN5ABnDQmmFBtO8YTYY/jtWIgCNET\nPMECC/whQYDlUxSUdbAhtkU4RNFOLBHZHyIHxZnExO0VRtBMWEUySWwHIbZtESthCIuGwy5YITW7\nnNZlcYrDJLuQJx+6QMMwx767p6a6r0sqbVfNW/Xete909W+feru2xhgBAGBnHrHoAQAAlpmYAgBo\nEFMAAA1iCgCgQUwBADSIKQCABjEFHAhVdWdV/fKi5wD2HzEFANAgpgAAGsQUcFNU1Wuq6pNV9aWq\nureqTlTVX6+qP6qqz1fVp6rqX1bVoYvu8/+q6mer6uNV9cWq+uWq+s6q+i9V9YWqevvXtq+q51TV\nA1X1uqqaVdUnquplV5nnR6vqnvm+31tV33cz/j0A+4+YAvZcVT01ySuS/OAY42iS5yWZJvlKkn+Y\n5FuTPDPJ30zyDy65+99K8gNJnpHkHyX5jSQvS/KXk3xfkpdetO0T54/1pCQ/neRNVfXdl5nnB5Js\nJHn5fPvfSPLOqjrcfrLAgSOmgJvhq0mOJPneqjo0xrh/jPE/xxj3jDHeN7bdn+RNSZ5zyX1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u2bqmpl0UNxfcYYn07yz5Pcn+RTSb4wxnj3YqdiB54wxngw2V5cSPKE\na93Bh3ZyRVX1mCR3JXnVfIWKW1xVvSDJg/OVxZpfWB6HktyW5F+NMW5L8uVsv+XAEqiqx2V7VWOS\n5ElJHlNVL1vsVOyCa/6H6V7F1KeSPOWi60+e38aSmC9R35XkLWOM31n0PFy3ZyV5YVV9IslmkhNV\n9dsLnonr98kkD4wx3j+/fle244rl8MNJPjHG+NwY46tJ/n2Sv7HgmbhxD1bVtyVJVT0xyZ9c6w57\nFVNnk/yVqprMf5PhJUn8VtFy+a0kHx1jvHHRg3D9xhivH2M8ZYzxndn+vnvPGOMnFz0X12f+1sID\nVfXU+U3PjV8kWCb3J3lGVX1LVVW2j59fILj1XbqK/84kPz3/+qeSXHNBYccf2nk1PtBzuVXVs5L8\neJIPVdU92V7ifP0Y4/cXOxkcCK9M8taqOpzkE0l+ZsHzcJ3GGO+rqruS3JPk4fk/37TYqbiaqnpb\nkvUkf6Gq7k9yR5JfSfKOqvq7Sc4nefE1H8eHdgIA7JwT0AEAGsQUAECDmAIAaBBTAAANYgoAoEFM\nAQA0iCkAgAYxBQDQ8P8BVsjtfzV4/yAAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x1765ee23eb8>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 将matplotlib的图表直接嵌入到Notebook之中。\n",
    "%matplotlib inline  \n",
    "\n",
    "# 首先，导入需要的库。\n",
    "\n",
    "import matplotlib.pyplot as plt  \n",
    "import numpy as np  \n",
    "\n",
    "# 构造我们的样本集合，每个样本可以表示成二维直角坐标平面上一个点(x1,x2)。\n",
    "\n",
    "x1 = np.array([1, 2, 3, 1, 5, 6, 5, 5, 6, 7, 8, 9, 9])  \n",
    "x2 = np.array([1, 3, 2, 2, 8, 6, 7, 6, 7, 1, 2, 1, 3])  \n",
    "x = np.array(list(zip(x1, x2))).reshape(len(x1), 2)  \n",
    "\n",
    "# 将样本集合可视化，结果如下图所示。\n",
    "\n",
    "plt.figure(figsize=(10,10))  \n",
    "plt.xlim([0,10]) \n",
    "plt.ylim([0,10])\n",
    "plt.title('sample')  \n",
    "plt.scatter(x1,x2)  \n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "从这张图里我们可以看到所有 13 个样本点的大致分布情况。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(0, 10)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAEACAYAAACj0I2EAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAADwdJREFUeJzt3X+M3HWdx/HXq6xGKIjmEjTbhrW3F+VCwpmNHHDEcxps\nMBjhEu4Mwp6WS/xLC6mcoZKsu+v+40VMQzz5g4itaLnL0ZIAxlNKmrkET0BSlAoFL+24he5RY+48\nafuP0Pf9sVNmu6Hd7nxn9/Pd9zwfyYTZYX6880158pnv7PTjiBAAYOVbVXoAAEBvEHQASIKgA0AS\nBB0AkiDoAJAEQQeAJBYMuu37bR+x/fyc295r+3HbL9v+ie0Ll3ZMAMBCzmaFvk3StfNu2yLpiYj4\nkKQ9kr7S68EAAIvjs/like0hSY9FxGXtn1+S9LGIOGL7/ZKaEXHJ0o4KADiTbs+hXxQRRyQpIl6T\ndFHvRgIAdKNXH4ry9wcAQGEDXT7uiO33zTnl8tvT3dE2sQeALkSEF3P/s12hu3056VFJG9vXPyfp\nkQWG4hKh8fHx4jPU5cKx4FhwLM586cbZ/Nrig5L+U9IHbR+yfaukr0vaYPtlSde0fwYAFLTgKZeI\nuPk0/+rjPZ4FAFAB3xRdRo1Go/QItcGx6OBYdHAsqjmr30Ov9AJ2LPVrAEA2thVL9KEoAKDmCDoA\nJEHQASA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iz1cER2++TJNvvl/TbhR6wVEH/uaQ/\nsz3U/rT6Jkn9/BsN35X0YkTcU3qQkiLiroi4OCL+VLN/JvZExGdLz1VC+630K7Y/2L7pGvXvB8WH\nJF1p+122rdlj0Y8fEM9/1/qopI3t65+TtOBisOsvFp0JXzrqsH21pFsk7bP9nGbfNt0VET8uOxlq\n4DZJO2y/Q9JBSbcWnqeIiHjG9k5Jz0n6Y/uf95WdannZflBSQ9Kf2D4kaVzS1yU9ZPsfJE1L+vSC\nz8MXiwAgBz4UBYAkCDoAJEHQASAJgg4ASRB0AEiCoANAEgQdAJIg6ACQxP8DuoXzyqYBg6cAAAAA\nSUVORK5CYII=\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x17666bc8a20>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 导入 K-means相关的库。\n",
    "\n",
    "from sklearn.cluster import KMeans\n",
    "import matplotlib.pyplot as plt  \n",
    "\n",
    "\n",
    "# 进行 K-Means聚类，设置聚类个数为 3。\n",
    "\n",
    "kmeans_model = KMeans(n_clusters=3).fit(x)\n",
    "\n",
    "# 将结果可视化，用三种形状和颜色表示三个簇。\n",
    "\n",
    "colors = ['b','g','r'] \n",
    "markers = ['o','^','+'] \n",
    "for i,j in enumerate(kmeans_model.labels_):  \n",
    "    plt.plot(x1[i],x2[i],color=colors[j],\n",
    "               marker=markers[j],ls='None')\n",
    "plt.xlim([0,10])    \n",
    "plt.ylim([0,10]) \n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可以看到，K-Means聚类将样本较有效的分成了 3 个类别。需要注意的是，K-Means 只能发现凸面形状的簇，如果簇的形状是非凸的，则效果很差。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
